package com.hjl.spring6.tx.service;

import com.hjl.spring6.tx.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ServivE
 * @version 0.1
 * @className BookServiceImpl
 * @description
 * @date 2024/8/7 20:48
 * @since jdk11
 */
@Service
@Transactional
public class BookServiceImpl implements BookService{
    //注入dao
    @Autowired
    private BookDao bookDao;
    //买书的方法:图书id和用户id
    @Override
    public void buyBook(Integer bookId, Integer userId) {
        //根据图书id查询图书价格
        Integer price = bookDao.selectPriceByBookId(bookId);
        //更新图书表库存量-1
        bookDao.updateStock(bookId);
        //更新用户表用户余额-图书价格
        bookDao.updateUserBalance(userId,price);
    }
}
